package com.huawei.quartz.conf;

import com.huawei.quartz.bean.QuartzData;
import com.huawei.quartz.job.MyQuartzJob;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

//@Configuration
public class QuartzConfig {
    @Value("${cron:0/1 * * * * ?}")
    private String cronSchedule;
    // 定义 JobDetail
    @Bean
    public JobDetail myJobDetail() {
        return JobBuilder.newJob(MyQuartzJob.class)
                .withIdentity("myQuartzJob") // 任务标识
                .storeDurably() // 保持任务状态
                .usingJobData("QuartzData", "")
                .build();
    }

    // 定义 Trigger
    @Bean
    public Trigger myJobTrigger() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronSchedule);
        // 使用 Cron 表达式定义调度规则
        return TriggerBuilder.newTrigger()
                .forJob(myJobDetail())
                .withIdentity("myQuartzTrigger")
                .withSchedule(cronScheduleBuilder) // 每10秒执行一次
                .build();
    }

    @Bean
    public QuartzData quartzData() {
        QuartzData quartzData = new QuartzData();
        quartzData.setJobName("123");
        quartzData.setTriggerName("123");
        quartzData.setExcuteTime(LocalDateTime.now());
        quartzData.setLastExcuteTime(LocalDateTime.now());
        quartzData.setExcuteCount(0);

        return quartzData;
    }
}
